package com.xiaomaoguai.common.utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * 通用工具类
 *
 */
public class CommonUtil {

	// 组成证书的字符
	public static final char[] BASE_CHAR = { '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
			'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
			'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
			'x', 'y', 'z' };

	public static final NumberFormat NUMBER_FOMATTER = new DecimalFormat("0.00");

	public static final SimpleDateFormat DATE_FORMATER = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	/**
	 * 生成证书
	 * 
	 * @return
	 */
	public static String genCertificate() {
		UUID uuid = UUID.randomUUID();
		String id = uuid.toString().replaceAll("-", "");
		StringBuilder re = new StringBuilder(24);
		String[] newSeq = new String[6];
		for (int i = 0; i < 5; ++i) {
			newSeq[i] = id.substring(i * 5, i * 5 + 5);
			int x = Integer.parseInt(newSeq[i], 16);
			re.append(covNumetic(x, 62));
		}
		newSeq[5] = id.substring(30);
		int x = Integer.parseInt(newSeq[5], 16);
		re.append(covNumetic(x, 62));
		return re.toString();
	}

	/**
	 * 按照指定进制转换数字
	 * 
	 * @param x
	 * @param radix
	 * @return
	 */
	static String covNumetic(long x, int radix) {
		StringBuilder res = new StringBuilder(5);
		long y = 0;
		long z = 0;
		while ((y = x / radix) >= radix) {
			z = x % radix;
			x = y;
			char tmp = BASE_CHAR[(int) z];
			res.insert(0, tmp);
		}

		if (x / radix == 0) {
			res.insert(0, BASE_CHAR[(int) (x % radix)]);
		} else {
			res.insert(0, BASE_CHAR[(int) (x % radix)]);
			res.insert(0, BASE_CHAR[(int) (x / radix)]);
		}
		return res.toString();
	}

	/**
	 * 计算百分比
	 */
	public static String percent(double x, double base) {
		if (base == 0) {
			return "0.00%";
		}
		Double result = new Double(x / base);
		String xx = NUMBER_FOMATTER.format(result * 100);
		return xx + "%";
	}

	public static String percent(float value) {
		String xx = NUMBER_FOMATTER.format(value * 100);
		return xx + "%";
	}

	/**
	 * 拆分时间
	 */
	public static String toTimeString(Date beg, Date end) {
		long interval = end.getTime() - beg.getTime();
		interval /= 1000;
		long hour = interval / 3600;
		long min = (interval % 3600) / 60;
		long sec = (interval % 3600) % 60;

		return hour + ":" + min + ":" + sec;
	}

	/**
	 * 格式化时间
	 * 
	 * @param interval
	 *            时间值， 单位是毫秒
	 * @return
	 */
	public static String formatTime(long interval) {
		interval /= 1000;
		long hour = interval / 3600;
		long min = (interval % 3600) / 60;
		long sec = (interval % 3600) % 60;

		StringBuilder result = new StringBuilder(10);
		if (hour < 10) {
			result.append("0");
		}
		result.append(hour).append(":");
		if (min < 10) {
			result.append("0");
		}
		result.append(min).append(":");
		if (sec < 10) {
			result.append("0");
		}
		result.append(sec);
		return result.toString();

	}

	/**
	 * 计算给定时间src加上interval小时后的时间
	 * 
	 * @param src
	 * @param interval
	 *            时间间隔
	 * @return
	 */
	public static Date dateAdd(Date src, int interval) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(src);
		calendar.add(Calendar.HOUR, interval);
		return calendar.getTime();
	}

	/**
	 * 转换文件大小
	 */
	public static String convertFileSize(double fileSize) {
		DecimalFormat format = new DecimalFormat("##0.00");
		if (fileSize >= 1024 * 1024) {
			return format.format(fileSize / (1024 * 1024)) + "MB";
		} else if (fileSize >= 1024) {
			return format.format(fileSize / 1024) + "KB";
		} else {
			return format.format(fileSize) + "B";
		}
	}

	private static final int[] X = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,
			5, 8, 4, 2 };
	private static final char[] C = { '1', '0', 'X', '9', 'A', '7', 'M', '5',
			'G', '3', 'J' };

	// 声称有效期校验位
	public static char validTimeEnc(long time, int jobId) throws Exception {
		int begIndex = (int) jobId % X.length;
		char[] charArr = (time + "").toCharArray();
		long res = 0;
		for (char ch : charArr) {
			res += ch * X[begIndex];
			begIndex = (++begIndex % X.length);
		}
		int mod = (int) (res % C.length);
		return C[mod];
	}

	private static final long MAX = 10000000000000000L;

	/**
	 * @param id
	 * @param precisiion
	 *            精度,即校验码的位数
	 * @return
	 */
	public static char[] checkCode(long id, int precisiion) {
		// 将id补全到17位
		StringBuilder l = new StringBuilder(X.length);
		if (id < MAX) {
			int len = ("" + id).length();
			while (l.length() < X.length - len) {
				len = (len++) % X.length;
				l.append(X[len]);
			}
		}
		l.append(id);

		char[] result = new char[precisiion];
		for (int i = 0; i < precisiion; ++i) {
			String s = id + "" + i;
			int begIndex = Math.abs(s.hashCode()) % X.length;
			char[] charArr = l.toString().toCharArray();
			long res = 0;
			for (char ch : charArr) {
				res += ch * X[begIndex++ % X.length];
			}
			int mod = (int) (res % C.length);
			result[i] = C[mod];
		}

		return result;
	}

	public static void main(String[] args) throws Exception {
		System.out.println(convertFileSize(1024));
	}
}
